species_name_list=['Tomato','Potato','viarum','lyratum','melongena','Pepper','biflora','Golden','Datura','Wolfberry']
import os
import sys
import pandas as pd
import seaborn as sns
import matplotlib
matplotlib.rcParams['pdf.fonttype']=42
import matplotlib.pyplot as plt
out_list=['CACTA_TIR_transposon','Copia_LTR_retrotransposon','Gypsy_LTR_retrotransposon','hAT_TIR_transposon','helitron','Mutator_TIR_transposon'\
    ,'PIF_Harbinger_TIR_transposon','Tc1_Mariner_TIR_transposon']
out_file=open(sys.argv[2],'w')
out_file.write('TE\tgroup\tvalue\tspecies\n')
for i in os.listdir(sys.argv[1]):
    if i.endswith('TE.inter.count'):
        # print(i)
    # if i.endswith('_TE.up.count_5K'):
        if 'trans' in i:
            type='2trans'
        elif 'loss' in i:
            type='3loss'
        elif 'syn' in i:
            type='1syn'
        file1=open(i,'r')
        for line in file1:
            line_list=line.strip().split('\t')
            if line_list[0] in out_list:
                out_file.write(line_list[0]+'\t'+type+'\t'+line_list[1]+'\t'+i.split('_')[0]+'\n')
        file1.close()

# file3=open(sys.argv[3],'r') # genome
# for line in file3:
#     line_list=line.strip().split('\t')
#     for i in line_list[1:]:
#         out_file.write(line_list[0]+'\tgenome\t'+str(float(i))+'\n')
    
out_file.close()
data1 = pd.read_csv(sys.argv[2],sep='\t')
fig, ax1 = plt.subplots(figsize=(15,10))
# print(help(sns.boxplot))
colors = ['blue', 'green', 'red',]
boxprops = dict(facecolor='white', edgecolor=colors)
medianprops = dict(color=colors)
whiskerprops = dict(color=colors)
sns.boxplot(x="TE", y="value", hue="group",data=data1.sort_values(by=['TE','group'] ))
# for patch in ax.artists:
#     hue_color = patch.get_facecolor()
#     patch.set_edgecolor(hue_color)
plt.ylim([0,0.3])

plt.xticks(rotation=30)  
plt.savefig(sys.argv[2]+'.pdf')  
    